package com.rabbiter.library.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rabbiter.library.annotation.Auth;
import com.rabbiter.library.utils.ResultUtils;
import com.rabbiter.library.utils.ResultVo;
import com.rabbiter.library.dto.CategoryEcharts;
import com.rabbiter.library.dto.ListCateParm;
import com.rabbiter.library.entity.SysCategory;
import com.rabbiter.library.service.SysCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "图书分类管理接口")
@RestController
@RequestMapping("/api/category")
public class SysCategoryController {
    @Autowired
    private SysCategoryService sysCategoryService;

    @ApiOperation(value = "添加分类", notes = "添加新的图书分类")
    @Auth
    @PostMapping
    public ResultVo add(@RequestBody SysCategory category){
        boolean save = sysCategoryService.save(category);
        if(save){
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    @ApiOperation(value = "编辑分类", notes = "根据ID更新图书分类信息")
    @Auth
    @PutMapping
    public ResultVo edit(@RequestBody SysCategory category){
        boolean save = sysCategoryService.updateById(category);
        if(save){
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    @ApiOperation(value = "删除分类", notes = "根据ID删除图书分类")
    @ApiImplicitParam(name = "categoryId", value = "分类ID", required = true, dataTypeClass = Long.class, paramType = "path")
    @Auth
    @DeleteMapping("/{categoryId}")
    public ResultVo delete(@PathVariable("categoryId") Long categoryId){
        boolean remove = sysCategoryService.removeById(categoryId);
        if(remove){
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    @ApiOperation(value = "获取分类列表", notes = "分页查询图书分类信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "categoryName", value = "分类名称", dataTypeClass = String.class)
    })
    @Auth
    @GetMapping("/list")
    public ResultVo getList(ListCateParm parm){
        IPage<SysCategory> list = sysCategoryService.getList(parm);
        return ResultUtils.success("查询成功",list);
    }

    @ApiOperation(value = "获取所有分类", notes = "获取所有图书分类，用于下拉选择")
    @Auth
    @GetMapping("/cateList")
    public ResultVo getCateList(){
        List<SysCategory> list = sysCategoryService.list();
        return ResultUtils.success("查询成功",list);
    }

    @ApiOperation(value = "获取分类统计", notes = "获取各个分类的图书数量统计")
    @Auth
    @GetMapping("/categoryCount")
    public ResultVo categoryCount(){
        CategoryEcharts categoryVo = sysCategoryService.getCategoryVo();
        return ResultUtils.success("查询成功",categoryVo);
    }
}
